import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../views/index'
import particulars  from "../views/particulars";
import Mine from "../views/Mine";
import {getUserInfo} from "../api/api";
import register from "../components/MytopSon/register";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    component: index
  },
  {
    path: '/particulars',
    name: 'particulars',
    component: particulars
  },
  {
    path: '/Mine',
    name: 'Mine',
    component: Mine,
    meta:{
      needLogin:true
    }
  },
  // {
  //   path: '/register',
  //   name: 'register',
  //   component: register,
  // },
  {
    path: '/register',
    name: 'register',
    component: register
  },


]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,


  routes
})

router.beforeEach((to, from, next,) => {
  // ...
  if(to.meta.needLogin){
    getUserInfo().then(res=>{
      if(res.code ==0){
        next()
      }else {
        next('/')
      }
    })
  }else {
    next()
  }
})

export default router
